#Boa:Frame:cadFuncFrame

from CONTROLE import *
import wx
import wx.grid

def create(parent):
    return cadFuncFrame(parent)

[wxID_CADFUNCFRAME, wxID_CADFUNCFRAMEALTERARBTN, wxID_CADFUNCFRAMECANCELARBTN, 
 wxID_CADFUNCFRAMECOMBOBOX1, wxID_CADFUNCFRAMEEXCLUIRBTN, 
 wxID_CADFUNCFRAMELIMPARBTN, wxID_CADFUNCFRAMELISTBOX1, 
 wxID_CADFUNCFRAMENOVOBTN, wxID_CADFUNCFRAMEPESQUISABTN, 
 wxID_CADFUNCFRAMESALVARBTN, wxID_CADFUNCFRAMESTATICBOX1, 
 wxID_CADFUNCFRAMESTATICBOX2, wxID_CADFUNCFRAMESTATICBOX3, 
 wxID_CADFUNCFRAMESTATICTEXT1, wxID_CADFUNCFRAMESTATICTEXT10, 
 wxID_CADFUNCFRAMESTATICTEXT2, wxID_CADFUNCFRAMESTATICTEXT3, 
 wxID_CADFUNCFRAMESTATICTEXT4, wxID_CADFUNCFRAMESTATICTEXT5, 
 wxID_CADFUNCFRAMESTATICTEXT6, wxID_CADFUNCFRAMESTATICTEXT7, 
 wxID_CADFUNCFRAMESTATICTEXT8, wxID_CADFUNCFRAMESTATICTEXT9, 
 wxID_CADFUNCFRAMETEXTCTRL1, wxID_CADFUNCFRAMETEXTCTRL2, 
 wxID_CADFUNCFRAMETEXTCTRL3, wxID_CADFUNCFRAMETEXTCTRL4, 
 wxID_CADFUNCFRAMETXTPESQUISA, 
] = [wx.NewId() for _init_ctrls in range(28)]

class cadFuncFrame(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_CADFUNCFRAME, name=u'cadFuncFrame',
              parent=prnt, pos=wx.Point(628, 120), size=wx.Size(566, 489),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Cadastro de Funcionarios')
        self.SetClientSize(wx.Size(550, 451))

        self.staticBox1 = wx.StaticBox(id=wxID_CADFUNCFRAMESTATICBOX1,
              label=u'Controle', name=u'staticBox1', parent=self,
              pos=wx.Point(408, 48), size=wx.Size(128, 224), style=0)

        self.staticBox2 = wx.StaticBox(id=wxID_CADFUNCFRAMESTATICBOX2,
              label=u'Pesquisa', name='staticBox2', parent=self, pos=wx.Point(8,
              280), size=wx.Size(528, 164), style=0)

        self.staticBox3 = wx.StaticBox(id=wxID_CADFUNCFRAMESTATICBOX3,
              label=u'Cadastro de Funcionarios', name='staticBox3', parent=self,
              pos=wx.Point(8, 48), size=wx.Size(384, 224), style=0)

        self.novoBtn = wx.Button(id=wxID_CADFUNCFRAMENOVOBTN, label=u'Novo',
              name=u'novoBtn', parent=self, pos=wx.Point(432, 80),
              size=wx.Size(75, 23), style=0)
        self.novoBtn.Enable(True)
        self.novoBtn.Bind(wx.EVT_BUTTON, self.OnNovoBtnButton,
              id=wxID_CADFUNCFRAMENOVOBTN)

        self.alterarBtn = wx.Button(id=wxID_CADFUNCFRAMEALTERARBTN,
              label=u'Alterar', name=u'alterarBtn', parent=self,
              pos=wx.Point(432, 112), size=wx.Size(75, 23), style=0)
        self.alterarBtn.Bind(wx.EVT_BUTTON, self.OnAlterarBtnButton,
              id=wxID_CADFUNCFRAMEALTERARBTN)

        self.salvarBtn = wx.Button(id=wxID_CADFUNCFRAMESALVARBTN,
              label=u'Salvar', name=u'salvarBtn', parent=self, pos=wx.Point(432,
              144), size=wx.Size(75, 23), style=0)
        self.salvarBtn.Bind(wx.EVT_BUTTON, self.OnSalvarBtnButton,
              id=wxID_CADFUNCFRAMESALVARBTN)

        self.cancelarBtn = wx.Button(id=wxID_CADFUNCFRAMECANCELARBTN,
              label=u'Cancelar', name=u'cancelarBtn', parent=self,
              pos=wx.Point(432, 176), size=wx.Size(75, 24), style=0)
        self.cancelarBtn.Bind(wx.EVT_BUTTON, self.OnCancelarBtnButton,
              id=wxID_CADFUNCFRAMECANCELARBTN)

        self.excluirBtn = wx.Button(id=wxID_CADFUNCFRAMEEXCLUIRBTN,
              label=u'Excluir', name=u'excluirBtn', parent=self,
              pos=wx.Point(432, 208), size=wx.Size(75, 23), style=0)
        self.excluirBtn.Bind(wx.EVT_BUTTON, self.OnExcluirBtnButton,
              id=wxID_CADFUNCFRAMEEXCLUIRBTN)

        self.staticText1 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT1,
              label=u'Cadastro de Funcionarios', name='staticText1',
              parent=self, pos=wx.Point(157, 8), size=wx.Size(236, 25),
              style=0)
        self.staticText1.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))
        self.staticText1.Center(wx.HORIZONTAL)

        self.pesquisaBtn = wx.Button(id=wxID_CADFUNCFRAMEPESQUISABTN,
              label=u'Pesquisa', name=u'pesquisaBtn', parent=self,
              pos=wx.Point(416, 296), size=wx.Size(56, 23), style=0)
        self.pesquisaBtn.Bind(wx.EVT_BUTTON, self.OnPesquisaBtnButton,
              id=wxID_CADFUNCFRAMEPESQUISABTN)

        self.limparBtn = wx.Button(id=wxID_CADFUNCFRAMELIMPARBTN,
              label=u'Limpar', name=u'limparBtn', parent=self, pos=wx.Point(480,
              296), size=wx.Size(40, 23), style=0)
        self.limparBtn.Bind(wx.EVT_BUTTON, self.OnLimparBtnButton,
              id=wxID_CADFUNCFRAMELIMPARBTN)

        self.txtPesquisa = wx.TextCtrl(id=wxID_CADFUNCFRAMETXTPESQUISA,
              name=u'txtPesquisa', parent=self, pos=wx.Point(24, 296),
              size=wx.Size(384, 21), style=0, value=u'')

        self.staticText3 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT3,
              label=u'Nome', name='staticText3', parent=self, pos=wx.Point(32,
              112), size=wx.Size(28, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT4,
              label=u'CPF', name='staticText4', parent=self, pos=wx.Point(32,
              144), size=wx.Size(20, 13), style=0)

        self.staticText5 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT5,
              label=u'Senha', name='staticText5', parent=self, pos=wx.Point(224,
              144), size=wx.Size(31, 13), style=0)

        self.staticText6 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT6,
              label=u'Cargo', name='staticText6', parent=self, pos=wx.Point(32,
              176), size=wx.Size(30, 13), style=0)

        self.comboBox1 = wx.ComboBox(choices=["Gerente", "Caixa"],
              id=wxID_CADFUNCFRAMECOMBOBOX1, name=u'comboBox1', parent=self,
              pos=wx.Point(80, 168), size=wx.Size(136, 21), style=0, value=u'')
        self.comboBox1.SetLabel(u'')
        self.comboBox1.Enable(False)

        self.textCtrl2 = wx.TextCtrl(id=wxID_CADFUNCFRAMETEXTCTRL2,
              name=u'textCtrl2', parent=self, pos=wx.Point(80, 104),
              size=wx.Size(296, 21), style=0, value=u'')
        self.textCtrl2.SetEditable(False)
        self.textCtrl2.Enable(False)

        self.textCtrl3 = wx.TextCtrl(id=wxID_CADFUNCFRAMETEXTCTRL3,
              name=u'textCtrl3', parent=self, pos=wx.Point(80, 136),
              size=wx.Size(136, 21), style=0, value=u'')
        self.textCtrl3.SetEditable(False)
        self.textCtrl3.Enable(False)

        self.textCtrl4 = wx.TextCtrl(id=wxID_CADFUNCFRAMETEXTCTRL4,
              name=u'textCtrl4', parent=self, pos=wx.Point(264, 136),
              size=wx.Size(112, 21), style=0, value=u'')
        self.textCtrl4.SetEditable(False)
        self.textCtrl4.Enable(False)

        self.staticText2 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT2,
              label=u'Codigo', name='staticText2', parent=self, pos=wx.Point(32,
              80), size=wx.Size(34, 13), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_CADFUNCFRAMETEXTCTRL1,
              name=u'textCtrl1', parent=self, pos=wx.Point(80, 72),
              size=wx.Size(40, 21), style=0, value=u'')
        self.textCtrl1.SetEditable(False)
        self.textCtrl1.Enable(False)

        self.listBox1 = wx.ListBox(choices=[], id=wxID_CADFUNCFRAMELISTBOX1,
              name=u'listBox1', parent=self, pos=wx.Point(24, 344),
              size=wx.Size(496, 88), style=0)
        self.listBox1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBox1ListboxDclick,
              id=wxID_CADFUNCFRAMELISTBOX1)

        self.staticText7 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT7,
              label=u'Codigo', name='staticText7', parent=self, pos=wx.Point(24,
              328), size=wx.Size(34, 13), style=0)

        self.staticText9 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT9,
              label=u'CPF', name='staticText9', parent=self, pos=wx.Point(352,
              328), size=wx.Size(20, 13), style=0)

        self.staticText10 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT10,
              label=u'Cargo', name='staticText10', parent=self,
              pos=wx.Point(416, 328), size=wx.Size(30, 13), style=0)

        self.staticText8 = wx.StaticText(id=wxID_CADFUNCFRAMESTATICTEXT8,
              label=u'Descri\xe7\xe3o', name='staticText8', parent=self,
              pos=wx.Point(96, 328), size=wx.Size(47, 13), style=0)

    def __init__(self, parent):
        
        self._init_ctrls(parent)
        

    def OnNovoBtnButton(self, event):
        self.alterar_campos(True)
        self.textCtrl2.SetValue('')
        self.textCtrl3.SetValue('')
        self.textCtrl4.SetValue('')
        self.comboBox1.SetValue('')
        self.textCtrl1.SetLabel('')
        
        
    def OnAlterarBtnButton(self, event):
        self.alterar_campos(True)
        

        
    def alterar_campos(self, VF):
        self.textCtrl4.SetEditable(VF)
        self.textCtrl4.Enable(VF)
        self.textCtrl3.SetEditable(VF)
        self.textCtrl3.Enable(VF)
        self.textCtrl2.SetEditable(VF)
        self.textCtrl2.Enable(VF)
        self.comboBox1.Enable(VF)

    def OnSalvarBtnButton(self, event):
        c = Controle()
        nome = self.textCtrl2.GetValue()
        cpf = self.textCtrl3.GetValue()
        senha = self.textCtrl4.GetValue()
        cargo = self.comboBox1.GetValue()
        cod = self.textCtrl1.GetValue()
        
        atributos = [nome, cpf, senha, cargo, cod]
        print atributos
        
        validacao = c.salvar_funcionario(atributos)
        
        if validacao == True:
            self.alterar_campos(False)
            
            m = 'Salvo com Sucesso'
            c = 'Cadastro de Funcionarios'
            wx.MessageBox(m,c,wx.OK)
        
        

    def OnCancelarBtnButton(self, event):
        self.Close()

    def OnExcluirBtnButton(self, event):
        c = Controle()
        cod = self.textCtrl1.GetValue()
        validacao = c.remover_funcionario(cod)
        if validacao == True:
            
            self.alterar_campos(False)
            self.OnLimparBtnButton(event)
            m = 'Excluido com Sucesso'
            c = 'Cadastro de Funcionarios'
            wx.MessageBox(m,c,wx.OK)

    def OnPesquisaBtnButton(self, event):
        self.listBox1.Clear()
        nome = self.txtPesquisa.GetValue()
        c = Controle()
        lista = c.procurar_funcionario_nome(nome)
        
        def codd(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x
            
        def desc(x):
            x = str(x)
            while len(x)<80:
                x = x+' '
            return x
            
        def cpf(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x
            
        def cargo(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x
            

        for r in lista:
            addd = codd(+r[0])+desc(r[1])+cpf(r[2])+cargo(r[4])
            self.listBox1.Append(addd)
                   
    def OnLimparBtnButton(self, event):
        self.txtPesquisa.SetValue('')
        self.listBox1.Clear()

    def OnListBox1ListboxDclick(self, event):
        a = self.listBox1.GetStringSelection()
        cod = a[0:2]
        cod = cod.split(" ")
        cod = str(cod[0])
        
        c = Controle()
        atributos = c.procurar_funcionario_codigo(cod)
        
        self.textCtrl1.SetValue(str(atributos[0]))
        self.textCtrl2.SetValue(str(atributos[1]))
        self.textCtrl3.SetValue(str(atributos[2]))
        self.textCtrl4.SetValue(str(atributos[3]))
        self.comboBox1.SetLabel(str(atributos[4]))
        
        self.alterar_campos(False)
        
        
